package communicationMessages;

import java.util.Date;
import java.util.logging.Level;

import logger.LoggerWriter;

import data.Forum;
import data.ForumMessage;
import data.Member;
import data.MemberDataBase;
import data.SubForum;
import database.DB;

public class AddPostMessage extends Message {

	@Override
	public String getAnswer() {
		LoggerWriter logger = LoggerWriter.getLoggerWriter();

		String copyStr=this.content;
		String [] arr =new String [7];
		int endIndex,beginIndex;
		beginIndex=0;
		for(int i=0; i<7;i++){
			endIndex=copyStr.indexOf(delimiter);
			arr[i]=copyStr.substring(beginIndex, endIndex);
			copyStr=copyStr.substring(endIndex+1);
		}	

		String ans = "";
		String userName,subject,body,subForumId,threadMsgId,responseToMsgId,strIsThread;
		userName = arr[0];
		subForumId = arr[1];
		threadMsgId = arr[2];
		responseToMsgId = arr[3];
		subject = arr[4];
		body = arr[5];
		strIsThread = arr[6];
		if (!ForumMessage.isLegalMsg(subject, body)) 
		{
			logger.log("failed"+delimiter+"add tpost Msg wrong format"+delimiter, Level.WARNING);
			ans = "failed"+delimiter+"add tpost Msg wrong format"+delimiter;
		}
		else
		{
			int userId = DB.getUserId(userName);
			if (!DB.isUserExist(userId))
			{
				ans = "user dosen't exist in the system";
				return ans;
			}
			if (!DB.isSubForumExist(subForumId)){
				logger.log("subForum is null the number is " + subForumId , Level.WARNING);
				ans = "pathNotFound" + delimiter;
				return ans;
			}
			if (DB.isThreadExist(threadMsgId))
			{
				boolean flag = false;
				boolean isResponseToPost = true;
				if (strIsThread.equals("true"))
				{
					flag = true;
					isResponseToPost = false;
				}
				else
				{
					if (DB.isPostMsgExist(responseToMsgId))
					{
						flag = true;
					}
				}
				if (flag)
				{
					if (!DB.isUserBannedFromSubForum(userId,subForumId))
					{
						addToBannedUsersIfNeeded(userName,userId,subForumId);
						int postMsgId = DB.addPostMsg(userId,threadMsgId,responseToMsgId,subject,body,new Date(System.currentTimeMillis()),
								isResponseToPost);
						String msg = "popupMessage"+delimiter+userName+delimiter+subject+delimiter+body+delimiter;
						String webMsg = getAddPostWebPopupMsg(postMsgId);
						sendMsgToObserver(subForumId, msg,webMsg);
						sendRefreshPostMsg(threadMsgId,postMsgId);
						ans="addPostMessage"+delimiter+"success"+delimiter;
					}
					else
					{
						ans="addPostMessage"+delimiter+"Failed\nyou are banned from this subforum"+delimiter;
					}
				}
				else
				{
					ans = "pathNotFound" + delimiter;
					return ans;
				}
			}
			else 
			{
				ans="addPostMessage"+delimiter+"failed"+delimiter;
			}
		}
		return ans;
	}




	//	String userName,subject,body,subForumId,threadMsgId,responseToMsgId;
	//	userName = arr[0];
	//	subForumId = arr[1];
	//	threadMsgId = arr[2];
	//	responseToMsgId = arr[3];
	//	subject = arr[4];
	//	body = arr[5];
	//	
	//	//System.out.println("username:" +userName+" "+subForumId+" "+threadMsgId+" "+responseToMsgId+" "+subject+" "+body);
	//	MemberDataBase tUserDataBase = MemberDataBase.getMembersDataBase();
	//	Member user = tUserDataBase.getMember(userName);
	//	Forum forum = Forum.getForum();
	//	SubForum subForum = forum.getSubForum(Integer.valueOf(subForumId));
	//	if (subForum == null){
	//		logger.log("subForum is null the number is " + subForumId , Level.WARNING);
	//		ans = "failed"+delimiter+"subForum wasn't found"+delimiter;
	//		return ans;
	//	}
	//	if (subForum.addPostMsg(subject, body, user, new Date(System.currentTimeMillis()), Integer.valueOf(threadMsgId), Integer.valueOf(responseToMsgId)))
	//	{
	//		Observer.addNewMsg("popupMessage"+delimiter+userName+delimiter+subject+delimiter+body+delimiter);
	//		ans="addPostMessage"+delimiter+"success"+delimiter;
	//	}
	//	else 
	//	{
	//		ans="addPostMessage"+delimiter+"failed"+delimiter;
	//	}
}

